<?php
require_once ("../db/connectdatabase.php"); 
require_once('RedirectForward.php');

function insertCompareLoans($id, $bank, $kvaytoida, $amount, $amountmax, $salarypermonth, $hinhthuctraluong, $rate, $rate2, $typerate, $infokhoanvaytoida, $time, $tattoan, $dieukiencoban, $country, $tttrchan, $cungcap) {
	$con = new ConnectDB();
	
	$tbl = "compareloanspersional";
	$fn = array('idcom' => $id, 'bankid' => $bank, 'idloanmaxi' => $kvaytoida, 'amountloanmin' => $amount, 'amountloanmax' => $amountmax, 'salarypermounth' => $salarypermonth, 'hinhthuctraluong' => $hinhthuctraluong, 'interrestrate' => $rate, 'interrestrate2' => $rate2, 'typerate' => $typerate, 'loanmaxxitext' => $infokhoanvaytoida, 'timeloan' => $time, 'phitattoan' => $tattoan, 'conditionbasic' => $dieukiencoban, 'country' => $country, 'tttrchan' => $tttrchan, 'isbankphi' => $cungcap, 'datecreate' => time());
	return $con -> insert($tbl, $fn);
}

function updateCompareLoans($id, $bank, $kvaytoida, $amount, $amountmax, $salarypermonth, $hinhthuctraluong, $rate, $rate2, $typerate, $infokhoanvaytoida, $time, $tattoan, $dieukiencoban, $country, $tttrchan, $cungcap) {
	$con = new ConnectDB();
	
	$tbl = "compareloanspersional";
	$fn = array('bankid' => $bank, 'idloanmaxi' => $kvaytoida, 'amountloanmin' => $amount, 'amountloanmax' => $amountmax, 'salarypermounth' => $salarypermonth, 'hinhthuctraluong' => $hinhthuctraluong, 'interrestrate' => $rate, 'interrestrate2' => $rate2, 'typerate' => $typerate, 'loanmaxxitext' => $infokhoanvaytoida, 'timeloan' => $time, 'phitattoan' => $tattoan, 'conditionbasic' => $dieukiencoban, 'country' => $country, 'tttrchan' => $tttrchan, 'isbankphi' => $cungcap, 'datecreate' => time());
	$condition = array('idcom' => $id);
	return $con -> update($tbl, $fn, $condition);
}

$url = "admin/admin.php?content=quanlivay";

if(isset($_POST['acc'])) {
	$act = $_POST['acc'];
	if($act == 'add') {
		if(isset($_POST['bank']) && isset($_POST['khoanvaytoidacode']) && isset($_POST['form_amount']) && isset($_POST['form_amountmax']) && isset($_POST['form_salarypermonth']) && isset($_POST['hinhthuctraluongcode']) && isset($_POST['form_rate']) && isset($_POST['form_rate2']) && isset($_POST['typeratecode']) && isset($_POST['form_infokhoanvaytoida']) && isset($_POST['form_time']) && isset($_POST['form_tattoan']) && isset($_POST['form_conditionbasic']) && isset($_POST['countryname']) && isset($_POST['form_tattoantrchan']) && isset($_POST['form_nhacungcap'])) {
			
			$id = time(); 
			
			$bank = $_POST['bank'];
			$arrkhoanvaytoida = $_POST['khoanvaytoidacode'];
			$kvaytoida = "";
			for($i = 0; $i < count($arrkhoanvaytoida); $i++) {
				$valuecode = $arrkhoanvaytoida[$i];
				if($i == (count($arrkhoanvaytoida) -1)) {
					$kvaytoida .= $valuecode;
				} else {
					$kvaytoida .= $valuecode.",";
				}
			}
			
			$amount = $_POST['form_amount'];
			$amountmax = $_POST['form_amountmax'];
			$salarypermonth = $_POST['form_salarypermonth'];
			
			$arrhinhthuctraluong = $_POST['hinhthuctraluongcode'];
			$hinhthuctraluong = "";
			for($i = 0; $i < count($arrhinhthuctraluong); $i++) {
				$valuecode = $arrhinhthuctraluong[$i];
				if($i == (count($arrhinhthuctraluong) -1)) {
					$hinhthuctraluong .= $valuecode;
				} else {
					$hinhthuctraluong .= $valuecode.",";
				}
			}
			
			$rate = $_POST['form_rate'];
			$rate2 = $_POST['form_rate2'];
			
			$arrtyperate = $_POST['typeratecode'];
			$typerate = "";
			for($i = 0; $i < count($arrtyperate); $i++) {
				if($i == (count($arrtyperate) - 1)) {
					$typerate .= $arrtyperate[$i];
				} else {
					$typerate .= $arrtyperate[$i].",";
				}
			}
			$infokhoanvaytoida = $_POST['form_infokhoanvaytoida'];
			$time = $_POST['form_time'];
			$tattoan = $_POST['form_tattoan'];
			$dieukiencoban = $_POST['form_conditionbasic'];
			
			$countryarr = $_POST['countryname'];
			$country = "";
			for($i = 0; $i < count($countryarr); $i++) {
				if($i == (count($countryarr) - 1)) {
					$country .= $countryarr[$i];
				} else {
					$country .= $countryarr[$i].", ";
				}
			}
			
			$tttrchan = $_POST['form_tattoantrchan'];
			$cungcap = $_POST['form_nhacungcap'];
			
			
			$url .= '&&as=add-compare-rate';
			if(insertCompareLoans($id, $bank, $kvaytoida, $amount, $amountmax, $salarypermonth, $hinhthuctraluong, $rate, $rate2, $typerate, $infokhoanvaytoida, $time, $tattoan, $dieukiencoban, $country, $tttrchan, $cungcap)) {
				$url .= "&&mess=Cập nhật thành công";
			} else {
				$url .= "&&mess=Cập nhật thất bại, vui lòng thử lại!";
			}
		}
	} else if($act == 'edit') {
		if(isset($_POST['idh']) && isset($_POST['bank']) && isset($_POST['khoanvaytoidacode']) && isset($_POST['form_amount']) && isset($_POST['form_amountmax']) && isset($_POST['form_salarypermonth']) && isset($_POST['hinhthuctraluongcode']) && isset($_POST['form_rate']) && isset($_POST['form_rate2']) && isset($_POST['typeratecode']) && isset($_POST['form_infokhoanvaytoida']) && isset($_POST['form_time']) && isset($_POST['form_tattoan']) && isset($_POST['form_conditionbasic']) && isset($_POST['countryname']) && isset($_POST['form_tattoantrchan']) && isset($_POST['form_nhacungcap'])) {
			$id = $_POST['idh']; 
			
			$bank = $_POST['bank'];
			$arrkhoanvaytoida = $_POST['khoanvaytoidacode'];
			$kvaytoida = "";
			for($i = 0; $i < count($arrkhoanvaytoida); $i++) {
				$valuecode = $arrkhoanvaytoida[$i];
				if($i == (count($arrkhoanvaytoida) -1)) {
					$kvaytoida .= $valuecode;
				} else {
					$kvaytoida .= $valuecode.",";
				}
			}
			
			$amount = $_POST['form_amount'];
			$amountmax = $_POST['form_amountmax'];
			$salarypermonth = $_POST['form_salarypermonth'];
			
			$arrhinhthuctraluong = $_POST['hinhthuctraluongcode'];
			$hinhthuctraluong = "";
			for($i = 0; $i < count($arrhinhthuctraluong); $i++) {
				$valuecode = $arrhinhthuctraluong[$i];
				if($i == (count($arrhinhthuctraluong) -1)) {
					$hinhthuctraluong .= $valuecode;
				} else {
					$hinhthuctraluong .= $valuecode.",";
				}
			}
			
			$rate = $_POST['form_rate'];
			$rate2 = $_POST['form_rate2'];
			
			$arrtyperate = $_POST['typeratecode'];
			$typerate = "";
			for($i = 0; $i < count($arrtyperate); $i++) {
				if($i == (count($arrtyperate) - 1)) {
					$typerate .= $arrtyperate[$i];
				} else {
					$typerate .= $arrtyperate[$i].",";
				}
			}
			$infokhoanvaytoida = $_POST['form_infokhoanvaytoida'];
			$time = $_POST['form_time'];
			$tattoan = $_POST['form_tattoan'];
			$dieukiencoban = $_POST['form_conditionbasic'];
			
			$countryarr = $_POST['countryname'];
			$country = "";
			for($i = 0; $i < count($countryarr); $i++) {
				if($i == (count($countryarr) - 1)) {
					$country .= $countryarr[$i];
				} else {
					$country .= $countryarr[$i].", ";
				}
			}
			
			$tttrchan = $_POST['form_tattoantrchan'];
			$cungcap = $_POST['form_nhacungcap'];
				
			$url .= '&&as=edit-compare-rate&&id='.$id;
			if(updateCompareLoans($id, $bank, $kvaytoida, $amount, $amountmax, $salarypermonth, $hinhthuctraluong, $rate, $rate2, $typerate, $infokhoanvaytoida, $time, $tattoan, $dieukiencoban, $country, $tttrchan, $cungcap) == true) {
				$url .= "&&mess=Cập Nhật Thành Công";
			} else {
				$url .= "&&mess=Cập Nhật Thất Bại, vui lòng thử lại!";
			}
		}
	}
}

redirect($url);
?>